/**
 * Created by Mark_ren on 2017/5/23.
 * 今天心情不好，可能是因为阴天。
 */
var moment = require("moment");
var co = require('co');
var seqModule = require('../../../routes/createModules');
var seq = new seqModule();
var util = require('../../../common/util');

var subcribeController = {

    /**
     *  添加订阅
     * @param req
     * @param res
     */
    addSubcribeLog: function (req, res) {
        co(function () {
            var uuid = util.getUUID();
            req.query.id = uuid;
            req.query.createTime = util.getNowTime();
            seq.sortSubscribeLogModule.count({ where: { sortId: req.query.sortId, memberId: req.query.memberId } }).then(function (results) {
                if (results == 0) {
                    seq.sortSubscribeLogModule.create(req.query).then(function (result) {
                        res.status(200).json({
                            message: "添加成功",
                            result: result
                        });
                    });
                } else {
                    res.json({ 'success': false, 'msg': '已订阅' });
                }

            });

        }).catch(function (e) {
            res.json({ 'success': false, 'msg': '新增失败' });
            console.log(e);
        })

    },
    /**
     *  订阅信息 根据会员ID查询
     * @param req
     * @param res
     */
    getSublistByMemberId: function (req, res) {
        co(function () {
            var params = {};
            var paramT = {};
            params["memberId"] = req.query.memberId;
            if (req.query.item != null) {
                params["item"] = req.query.item;
            }
            paramT["where"] = params;
            paramT["order"] = [['item', 'ASC']];
            seq.sortSubscribeLogModule.findAll(paramT).then(function (result) {
                res.json({ "success": true, "msg": "查询成功！ ", "result": result });
            });
        }).catch(function (e) {
            res.json({ 'success': false, 'msg': '查询失败' });
            console.log(e);
        })

    },
    /**
     *  首页各模块订阅数量接口
     * @param req
     * @param res
     */
    getSubCountlist: function (req, res) {
        var memberSqlId = "";
        if (req.query.memberId != undefined) {
            memberSqlId += " and member_id = '" + req.query.memberId + "'";
        }
        var memberSql = "select dic.code_id as codeId,dic.code_desc as codeDesc, (select count(*) from lm_sort_subscribe_log where item = dic.code_id) as allCount,(select count(*) from lm_sort_subscribe_log where item = dic.code_id" + memberSqlId + ") as isSubscribe from lm_dic as dic where 1=1 ";
        memberSql += " and types = 20001 order by nums ASC"
        co(function () {
            seq.sequelize.query(memberSql).spread(function (result) {
                res.json({ "success": true, "result": result });
            });
        }).catch(function (e) {
            res.json({ 'success': false, 'msg': '查询失败' });
            console.log(e);
        })

    },
    unsubcribe: function (req, res) {
        seq.sortSubscribeLogModule.destroy({ where: req.params }).then(function (result) {
            res.json({ "success": true, "msg": "取消订阅成功！", "result": result });
        }).catch(function (e) {
            res.json({ 'success': false, 'msg': '取消订阅失败' });
            console.log(e);
        })
    }

}
module.exports = subcribeController;